/**
 * 
 */
package com.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.cms.model.AppPointEntry;
import com.cms.model.AppPointOfBilling;
import com.cms.model.AppPointTerraceInfo;
import com.service.AbstractDateService;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class AppPointOfBillingServiceImpl extends AbstractDateService implements AppPointOfBillingService
{
	private Logger logger;
	
	public AppPointOfBillingServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		//doSomeThing();
	}
		
	@Override
	public AppPointOfBilling getAppPointOfBilling(int id) 
	{
		String sql = "select * from t_apppointofbilling where id=" + String.valueOf(id) + "";
		ModelGenerator<AppPointOfBilling> dbcpmg = new ModelGenerator<AppPointOfBilling>(AppPointOfBilling.class);
		return dbcpmg.generate(base, sql);
	}

	@Override
	public List<AppPointOfBilling> getAppPointOfBillings()
	{
		String sql = "select * from t_apppointofbilling order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<AppPointOfBilling> dbcpmg = new ModelGenerator<AppPointOfBilling>(AppPointOfBilling.class);
		return dbcpmg.generateList(base, sql);
	}
	
	@Override
	public boolean saveAppPointOfBilling(String type, AppPointOfBilling model)
	{
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
			
			model.setId(getTableId("t_apppointofbilling"));
			sql = 
				"insert into t_apppointofbilling(id,entryid,code,cpid,appnumber,pointofbillingname,fee,type,triggerremark,propremark,remark,createuser,createdate) " +
				"values(" + String.valueOf(model.getId()) + ","+model.getEntryid()+",'"+model.getCode()+"','"+model.getCpid()+"','" 
				+ encodeDb(model.getAppnumber()) + "','" + encodeDb(model.getPointofbillingname()) + 
				"','" + model.getFee() + "','"+model.getType()+"','"+encodeDb(model.getTriggerremark())+"','"+encodeDb(model.getPropremark())+"','" +encodeDb(model.getRemark()) + "','" + model.getCreateuser() + "','" + 
						model.getCreatedate() + "')";
			
		}
		else if (type.equalsIgnoreCase("update"))
		{
			sql = 
				"update t_apppointofbilling set pointofbillingname='" + encodeDb(model.getPointofbillingname()) + "'," +
						"fee='" + model.getFee() + "'," +
						"entryid=" + model.getEntryid() + "," +
						"remark='" + encodeDb(model.getRemark()) + "'," +
								"type='"+model.getType()+"'," +
										"triggerremark='"+encodeDb(model.getTriggerremark())+"'," +
												"propremark='"+encodeDb(model.getPropremark())+"' " +
				" where id=" + String.valueOf(model.getId()) + "";
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "delete from t_apppointofbilling where id=" + String.valueOf(model.getId()) + "";
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (ConnectionManager.getInstance().execute(base, sql))
		{
			return true;
		}
		return false;
	}
	public List<AppPointOfBilling> getAppPointOfBillingOfAppNumber(String appnumber,String code)
	{
		String sql = "select * from t_apppointofbilling where appnumber='" +appnumber+ "' and code='"+code+"'";
		System.out.println(sql);
		ModelGenerator<AppPointOfBilling> dbcpmg = new ModelGenerator<AppPointOfBilling>(AppPointOfBilling.class);
		return dbcpmg.generateList(base, sql);
	}
	public List<AppPointOfBilling> getAppPointOfBillingOfAppNumber(String appnumber)
	{
		String sql = "select * from t_apppointofbilling where appnumber='" +appnumber+ "' order by id";
		ModelGenerator<AppPointOfBilling> dbcpmg = new ModelGenerator<AppPointOfBilling>(AppPointOfBilling.class);
		return dbcpmg.generateList(base, sql);
	}

	@Override
	public List<AppPointTerraceInfo> getByAppPointId(Integer appPointId) {
		String sql = "select  b.id as terraceId ,b.operatorsName, a.remark as glremark,a.terraceId,a.id as ptId,a.pointcode from "
				+"(select * from t_terrace ) b"
				+" left join t_apppointterrace a"
				+" on a.terraceId=b.id and a.apppointId="+appPointId+"" ;
			logger.debug("sql: " + sql);		
			ModelGenerator<AppPointTerraceInfo> dbcpmg = new ModelGenerator<AppPointTerraceInfo>(AppPointTerraceInfo.class);
			return dbcpmg.generateList(base, sql);
	}

	@Override
	public boolean updateAppPointTerraceInfo(AppPointOfBilling appPoint,
			List<AppPointTerraceInfo> appPointTerraceInfos) {
		if(appPointTerraceInfos==null){
			appPointTerraceInfos =new ArrayList<AppPointTerraceInfo>();
		}
		
		//更新应用版本信息
		this.saveAppPointOfBilling("update", appPoint);
		//更新应用版本平台的信息
		String sql = "delete from t_apppointterrace where apppointId=" + appPoint.getId();
		ConnectionManager.getInstance().execute(base, sql);
		
		for(AppPointTerraceInfo appPointTerraceInfo : appPointTerraceInfos){
			if (appPointTerraceInfo.getApppointId() <= 0)
				appPointTerraceInfo.setApppointId(appPoint.getId());
			this.insertAppPointTerraceInfo(appPointTerraceInfo);
		}
		
		return true;
	}

	@Override
	public boolean insertAppPointTerraceInfo(AppPointOfBilling appPoint,
			List<AppPointTerraceInfo> appPointTerraceInfos) {
		if(appPointTerraceInfos==null){
			appPointTerraceInfos =new ArrayList<AppPointTerraceInfo>();
		}
		//添加渠道信息
		this.saveAppPointOfBilling("insert", appPoint);
		//添加渠道平台关联信息
		for(AppPointTerraceInfo av: appPointTerraceInfos){
			av.setApppointId(appPoint.getId());
			this.insertAppPointTerraceInfo(av);
		}
		
		return true;
	}
	public boolean insertAppPointTerraceInfo(AppPointTerraceInfo appPointTerraceInfo){		
		appPointTerraceInfo.setPtId(getTableId("t_apppointterrace"));
		String sql = 
			"insert into t_apppointterrace(id,apppointId,pointcode,terraceId,remark) " +
			"values(" + String.valueOf(appPointTerraceInfo.getPtId()) + ","+String .valueOf(appPointTerraceInfo.getApppointId()) +",'"+appPointTerraceInfo.getPointCode()+"',"
					+String.valueOf(appPointTerraceInfo.getTerraceId())+ ",'"+ appPointTerraceInfo.getGlremark()+"')";
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public boolean deleteAppPoint(AppPointOfBilling appPoint) {
		String[] sql = {"delete from t_apppointofbilling where id=" + String.valueOf(appPoint.getId()) + "",
				"delete from t_apppointterrace where apppointId=" + String.valueOf(appPoint.getId()) + ""
		};
		return ConnectionManager.getInstance().executeBatch(base, sql);
	}

	@Override
	public List<AppPointTerraceInfo> queryAppPointTerrace(int appversionId) {
		String sql = "select  b.id as terraceId ,b.operatorsName,a.appversionId,a.apppointId,a.remark as glremark,a.terraceId,a.id as ptId,a.pointcode from t_terrace b"
			/*	+"(select * from t_terrace ) b"*/
				+" inner join t_apppointterrace a"
				+" on a.terraceId=b.id where a.appversionId="+String.valueOf(appversionId)+"";
			logger.debug("sql: " + sql);	
			ModelGenerator<AppPointTerraceInfo> dbcpmg = new ModelGenerator<AppPointTerraceInfo>(AppPointTerraceInfo.class);
			return dbcpmg.generateList(base, sql);
	}

	//根据应用的编号查找相对应的计费点
	@Override
	public List<AppPointOfBilling> queryAppPoint(String appnumber) {
		String sql = "select ap.id, ap.fee, ap.pointofbillingname,ap.code from t_appinfo  as a , t_apppointofbilling as ap "
				+"where  a.appnumber=ap.appnumber and  a.appnumber='"+appnumber+"'";
			logger.debug("sql: " + sql);	
			ModelGenerator<AppPointOfBilling> dbcpmg = new ModelGenerator<AppPointOfBilling>(AppPointOfBilling.class);
			return dbcpmg.generateList(base, sql);
	}

	@Override
	public AppPointOfBilling getgetAppPointOfBilling(String appversionnumber,
			String code) {
		String sql="select ap.* from t_apppointofbilling ap, t_appversioninfo av where ap.appnumber=av.appnumber and av.appversionnumber='"+appversionnumber+"' and ap.code='"+code+"'";
		ModelGenerator<AppPointOfBilling> dbcpmg = new ModelGenerator<AppPointOfBilling>(AppPointOfBilling.class);
		return dbcpmg.generate(base, sql);
	}

	@Override
	public List<AppPointEntry> getPointEntryByModel(AppPointEntry model) {
		String sql="select * from t_apppointentry where isdel='0'";
		if(model.getId()!=0){
			sql+=" and id="+model.getId();
		}
		if(null!=model.getEntrycode()&&!"".equals(model.getEntrycode())){
			sql+=" and entrycode='"+model.getEntrycode()+"'";
		}
		if(null!=model.getAppnumber()&&!"".equals(model.getAppnumber())){
			sql+=" and appnumber='"+model.getAppnumber()+"'";
		}
		sql+=" order by entrycode asc";
		ModelGenerator<AppPointEntry> dbcpmg = new ModelGenerator<AppPointEntry>(AppPointEntry.class);
		return dbcpmg.generateList(base, sql);
	}

	@Override
	public boolean savePointEntry(String type, AppPointEntry model) {
		String sql="";
		if("insert".equals(type)){
			model.setId(getTableId("t_apppointentry"));
			sql="insert into t_apppointentry(id,entrycode,entryname,triggerremark,appnumber) values ("+model.getId()+"," +
					"'"+model.getEntrycode()+"','"+model.getEntryname()+"','"+model.getTriggerremark()+"','"+model.getAppnumber()+"')";
		}else if("update".equals(type)){
			sql = "update t_apppointentry set entrycode='"+model.getEntrycode()+"',entryname='"+model.getEntryname()+"',triggerremark='"+model.getTriggerremark()+"' where id="+model.getId();
		}else if("delete".equals(type)){
			sql="update t_apppointentry set isdel='1' where id="+model.getId();
		}
		return ConnectionManager.getInstance().execute(base, sql);
	}
	
	public void doSomeThing(){
		String sql = "select * from t_apppointofbilling where appnumber!='AP4438AE5B3A76F0F198AC00B22AE96F42' and triggerremark!=''";
		ModelGenerator<AppPointOfBilling> dbcpmg = new ModelGenerator<AppPointOfBilling>(AppPointOfBilling.class);
		List<AppPointOfBilling> l = dbcpmg.generateList(base, sql);
		for (AppPointOfBilling appPointOfBilling : l) {
			AppPointEntry entry = new AppPointEntry();
			int tabid = getTableId("t_apppointentry");
			entry.setId(tabid);
			String sql1 = "select ifnull(max(entrycode),0) from t_apppointentry where appnumber='"+appPointOfBilling.getAppnumber()+"'";
			ModelGenerator<Integer> mg = new ModelGenerator<Integer>(Integer.class);
			int entrycode = mg.generate(base, sql1);
			entry.setEntrycode(String.valueOf(entrycode+1));
			entry.setAppnumber(appPointOfBilling.getAppnumber());
			entry.setTriggerremark(appPointOfBilling.getTriggerremark());
			entry.setEntryname("");
			String sql2="insert into t_apppointentry(id,entrycode,entryname,triggerremark,appnumber) values ("+entry.getId()+"," +
					"'"+entry.getEntrycode()+"','"+entry.getEntryname()+"','"+entry.getTriggerremark()+"','"+entry.getAppnumber()+"')";
			ConnectionManager.getInstance().execute(base, sql2);
			
			String sql3="update t_apppointofbilling set entryid="+tabid+" where id="+appPointOfBilling.getId();
			ConnectionManager.getInstance().execute(base, sql3);
		}
	}
	
}
